import pandas as pd
from matplotlib import pyplot as plt
plt.style.use('seaborn')
from sklearn.metrics import roc_curve,roc_auc_score

def ks_calc(y_predict_proba, y_true):
    try:
        fpr, tpr, thresholds = roc_curve(y_true, y_predict_proba)
        ks_value = max (abs (fpr - tpr))
        return ks_value
    except Exception as e:
        print (e)
        print ("无法计算ks值，请检查验证集和测试集是否标签全为 1 或 0，或有缺失值")
        exit (1)


def auc_calc(y_predict_proba, y_true):
    try:
        auc = roc_auc_score(y_true,y_predict_proba)
        return auc
    except Exception as e:
        print (e)
        print ("无法计算auc值，请检查验证集和测试集是否标签全为 1 或 0，或有缺失值")
        exit (1)

def print_hist(hist_data,title_name='relation',bins=20,save_path=None,show=False):
    hist_plot = pd.Series (hist_data)
    plt.cla ()
    hist_plot.plot (kind="hist", bins=bins)
    name = f"{title_name}.png"
    plt.title (name)
    if save_path is not None:
        plt.savefig(save_path+name)
    if show:
        plt.show ()